/**
 * Copyright (C) 2011, mordredd666 [_at_] gmail.com
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package de.mordred;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Props
{

	public static String RANDOM_FRAGEN = "random";
	public static String LIZENZ = "license";
	
	private static Props singleton;
	private static Object LOCK = new Object();

	private static String FILE = "jp.properties";

	private Properties _properties;

	/**
	 * Returns the Instance
	 * @return
	 */
	public static Props getInstance()
	{
		if (singleton == null)
		{
			synchronized (LOCK)
			{
				singleton = new Props();
			}
		}
		return singleton;
	}

	private Props()
	{
		_properties = loadProperties();
	}

	public String getString(String s)
	{

		if (_properties.get(s) != null)
			return (String) _properties.get(s);
		else
			return "";
	}

	public boolean getBoolean(String s)
	{
		if (_properties.get(s) != null)
		{
			return Boolean.parseBoolean(_properties.getProperty(s));
		}

		return false;

	}

	public void setProperty(String prop, String value)
	{
		_properties.setProperty(prop, value);
		save();
	}

	public void setProperty(String prop, boolean bool)
	{
		_properties.setProperty(prop, Boolean.toString(bool));
		save();
	}

	public void save()
	{
		try
		{
			_properties.store(new FileOutputStream(new File(FILE)), "Stored Properties");
		}
		catch (FileNotFoundException e)
		{
		}
		catch (IOException e)
		{
		}
	}

	private Properties loadProperties()
	{

		Properties props = new Properties();
		try
		{
			props.load(new FileInputStream(FILE));
		}
		catch (FileNotFoundException e)
		{
			File f = new File(FILE);
			try
			{
				f.createNewFile();
				props.load(new FileInputStream(f));
			}
			catch (IOException e1)
			{
			}

		}
		catch (IOException e)
		{

		}

		return props;
	}
}
